package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego/logs"
	services "tinyrdm/backend/services"
	"tinyrdm/backend/types"
)

var monitorLog = logs.GetLogger("Monitor")

type MonitorController struct {
	BaseController
}

type MonitorReqData struct {
	Method string   `json:"method"`
	Server string   `json:"server"`
	Logs   []string `json:"logs"`
}

var monitor = services.Monitor()

func (c *MonitorController) Handler() {
	monitor.Start(c.context())
	var req MonitorReqData
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		logs.Error(err, string(c.Ctx.Input.RequestBody))
		c.ErrorJson(err)
		return
	}
	var resp any
	switch req.Method {
	case "Start":

		break
	case "ExportLog":
		//这个方法要客户端实现
		resp = monitor.ExportLog(req.Logs)
		break
	case "StartMonitor":
		resp = monitor.StartMonitor(req.Server)
		break
	case "StopAll":
		resp = monitor.StopAll
		break
	case "StopMonitor":
		resp = monitor.StopMonitor(req.Server)
		break
	default:
		resp = types.JSResp{
			Success: false,
			Msg:     "未找到该方法",
			Data:    nil,
		}
		monitorLog.Printf("not found: %s", req.Method)
	}
	if resp == nil {
		resp = types.JSResp{
			Success: true,
			Msg:     "Successful",
			Data:    nil,
		}
	}
	c.postJson(resp)
}
